package com.liouwb.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author liouwb
 */
@RestController
@RequestMapping("provider")
public class ProviderController {


    @GetMapping("getProvider")
    public String getProvider() {
        return "get provider-hustrix";
    }


    @GetMapping("getProviderHystrix")
    @HystrixCommand(fallbackMethod = "hystrixCall")
    public String getProviderHystrix() {
        double num = Math.random() * 10;
        if (num > 5) {
            throw new RuntimeException("provider-server1出现异常");
        }
        return "get getProviderHystrix";
    }

    /**
     * 失败之后hystrix的降级方法
     *
     * @return
     */
    public String hystrixCall() {
        return "@Hystrix call resp";
    }
}
